Skip to main content

Create Current, Past and Future Dates

DevAssure provides a features called Advanced Code Blocks which allows users to write custom JavaScript code to generate dynamic data. This feature can be used to create current, past, and future dates that can be utilized in various scenarios such as scheduling events, setting deadlines, or validating date-related functionalities during test automation.

Learn more about Advanced Code Blocks

Function to create the date using a delta parameter

const options = {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
};

const currentDate = new Date();
const futureDate = new Date(currentDate);

if (delta != 0) {
futureDate.setDate(Number(currentDate.getDate()) + Number(delta));
}

const formattedFutureDate = futureDate.toLocaleString('en-US', options);

The code above defines a function to create a date based on a delta parameter, which represents the number of days to add or subtract from the current date. Here's a breakdown of how it works:

  1. Options Object: The options object specifies the formatting options for the date, including the month, day, year, hour, minute, and whether to use a 12-hour clock.

  2. Current Date: The currentDate variable holds the current date and time.

  3. Future Date: The futureDate variable is initialized to the current date. If the delta parameter is not zero, the code adjusts the day of the futureDate by adding the delta value.

  4. Formatted Date: The formattedFutureDate variable holds the formatted date string based on the specified options.

Function to create the date using a delta parameter

Sample DevAssure code

Sample DevAssure code